<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>style_test.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="local_closure_goog_cssom_iframe_style_test.js.html">style_test.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2010 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>goog.provide(&#39;goog.cssom.iframe.styleTest&#39;);
<a name="line16"></a>goog.setTestOnly(&#39;goog.cssom.iframe.styleTest&#39;);
<a name="line17"></a>
<a name="line18"></a>goog.require(&#39;goog.cssom&#39;);
<a name="line19"></a>goog.require(&#39;goog.cssom.iframe.style&#39;);
<a name="line20"></a>goog.require(&#39;goog.dom&#39;);
<a name="line21"></a>goog.require(&#39;goog.dom.DomHelper&#39;);
<a name="line22"></a>goog.require(&#39;goog.testing.jsunit&#39;);
<a name="line23"></a>goog.require(&#39;goog.userAgent&#39;);
<a name="line24"></a>
<a name="line25"></a>// unit tests
<a name="line26"></a>var propertiesToTest = [
<a name="line27"></a>  &#39;color&#39;,
<a name="line28"></a>  &#39;font-family&#39;,
<a name="line29"></a>  &#39;font-style&#39;,
<a name="line30"></a>  &#39;font-size&#39;,
<a name="line31"></a>  &#39;font-variant&#39;,
<a name="line32"></a>  &#39;border-top-style&#39;,
<a name="line33"></a>  &#39;border-top-width&#39;,
<a name="line34"></a>  &#39;border-top-color&#39;,
<a name="line35"></a>  &#39;background-color&#39;,
<a name="line36"></a>  &#39;margin-bottom&#39;
<a name="line37"></a>];
<a name="line38"></a>
<a name="line39"></a>function crawlDom(startNode, func) {
<a name="line40"></a>  if (startNode.nodeType != 1) { return; }
<a name="line41"></a>  func(startNode);
<a name="line42"></a>  for (var i = 0; i &lt; startNode.childNodes.length; i++) {
<a name="line43"></a>    crawlDom(startNode.childNodes[i], func);
<a name="line44"></a>  }
<a name="line45"></a>}
<a name="line46"></a>
<a name="line47"></a>function getCurrentCssProperties(node, propList) {
<a name="line48"></a>  var props = {};
<a name="line49"></a>  if (node.nodeType != 1) { return; }
<a name="line50"></a>  for (var i = 0; i &lt; propList.length; i++) {
<a name="line51"></a>    var prop = propList[i];
<a name="line52"></a>    if (node.currentStyle) { // IE
<a name="line53"></a>      var propCamelCase = &#39;&#39;;
<a name="line54"></a>      var propParts = prop.split(&#39;-&#39;);
<a name="line55"></a>      for (var j = 0; j &lt; propParts.length; j++) {
<a name="line56"></a>        propCamelCase += propParts[j].charAt(0).toUpperCase() +
<a name="line57"></a>                         propParts[j].substring(1, propParts[j].length);
<a name="line58"></a>      }
<a name="line59"></a>      props[prop] = node.currentStyle[propCamelCase];
<a name="line60"></a>    } else { // standards-compliant browsers
<a name="line61"></a>      props[prop] = node.ownerDocument.defaultView.getComputedStyle(
<a name="line62"></a>          node, &#39;&#39;).getPropertyValue(prop);
<a name="line63"></a>    }
<a name="line64"></a>  }
<a name="line65"></a>  return props;
<a name="line66"></a>}
<a name="line67"></a>
<a name="line68"></a>function CssPropertyCollector() {
<a name="line69"></a>  var propsList = [];
<a name="line70"></a>  this.propsList = propsList;
<a name="line71"></a>
<a name="line72"></a>  this.collectProps = function(node) {
<a name="line73"></a>    var nodeProps = getCurrentCssProperties(node, propertiesToTest);
<a name="line74"></a>    if (nodeProps) { propsList.push([nodeProps, node]); }
<a name="line75"></a>  };
<a name="line76"></a>}
<a name="line77"></a>
<a name="line78"></a>function recursivelyListCssProperties(el) {
<a name="line79"></a>  var collector = new CssPropertyCollector();
<a name="line80"></a>  crawlDom(el, collector.collectProps);
<a name="line81"></a>  return collector.propsList;
<a name="line82"></a>}
<a name="line83"></a>
<a name="line84"></a>function testMatchCssSelector() {
<a name="line85"></a>  var container = document.createElement(&#39;div&#39;);
<a name="line86"></a>  container.className = &#39;container&#39;;
<a name="line87"></a>  var el = document.createElement(&#39;div&#39;);
<a name="line88"></a>  x = el;
<a name="line89"></a>  el.id = &#39;mydiv&#39;;
<a name="line90"></a>  el.className = &#39;colorful foo&#39;;
<a name="line91"></a>  // set some arbirtrary content
<a name="line92"></a>  el.innerHTML = &#39;&lt;div&gt;&lt;ul&gt;&lt;li&gt;One&lt;/li&gt;&lt;li&gt;Two&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;&#39;;
<a name="line93"></a>  container.appendChild(el);
<a name="line94"></a>  document.body.appendChild(container);
<a name="line95"></a>
<a name="line96"></a>  var elementAncestry = new goog.cssom.iframe.style.NodeAncestry_(el);
<a name="line97"></a>  assertEquals(5, elementAncestry.nodes.length);
<a name="line98"></a>
<a name="line99"></a>  // list of input/output results. Output is the index of the selector
<a name="line100"></a>  // that we expect to match - for example, in &#39;body div div.colorful&#39;,
<a name="line101"></a>  // &#39;div.colorful&#39; has an index of 2.
<a name="line102"></a>  var expectedResults = [
<a name="line103"></a>    [&#39;body div&#39;, [4, 1]],
<a name="line104"></a>    [&#39;h1&#39;, null],
<a name="line105"></a>    [&#39;body div h1&#39;, [4, 1]],
<a name="line106"></a>    [&#39;body div.colorful h1&#39;, [4, 1]],
<a name="line107"></a>    [&#39;body div div&#39;, [4, 2]],
<a name="line108"></a>    [&#39;body div div div&#39;, [4, 2]],
<a name="line109"></a>    [&#39;body div div.somethingelse div&#39;, [4, 1]],
<a name="line110"></a>    [&#39;body div.somethingelse div&#39;, [2, 0]],
<a name="line111"></a>    [&#39;div.container&#39;, [3, 0]],
<a name="line112"></a>    [&#39;div.container div&#39;, [4, 1]],
<a name="line113"></a>    [&#39;#mydiv&#39;, [4, 0]],
<a name="line114"></a>    [&#39;div#mydiv&#39;, [4, 0]],
<a name="line115"></a>    [&#39;div.colorful&#39;, [4, 0]],
<a name="line116"></a>    [&#39;div#mydiv .colorful&#39;, [4, 0]],
<a name="line117"></a>    [&#39;.colorful&#39;, [4, 0]],
<a name="line118"></a>    [&#39;body * div&#39;, [4, 2]],
<a name="line119"></a>    [&#39;body * *&#39;, [4, 2]]
<a name="line120"></a>  ];
<a name="line121"></a>  for (var i = 0; i &lt; expectedResults.length; i++) {
<a name="line122"></a>    var input = expectedResults[i][0];
<a name="line123"></a>    var expectedResult = expectedResults[i][1];
<a name="line124"></a>    var selector = new goog.cssom.iframe.style.CssSelector_(input);
<a name="line125"></a>    var result = selector.matchElementAncestry(elementAncestry);
<a name="line126"></a>    if (expectedResult == null) {
<a name="line127"></a>      assertEquals(&#39;Expected null result&#39;, expectedResult, result);
<a name="line128"></a>    } else {
<a name="line129"></a>      assertEquals(&#39;Expected element index for &#39; + input,
<a name="line130"></a>                   expectedResult[0],
<a name="line131"></a>                   result.elementIndex);
<a name="line132"></a>      assertEquals(&#39;Expected selector part index for &#39; + input,
<a name="line133"></a>                   expectedResult[1],
<a name="line134"></a>                   result.selectorPartIndex);
<a name="line135"></a>    }
<a name="line136"></a>  }
<a name="line137"></a>  document.body.removeChild(container);
<a name="line138"></a>}
<a name="line139"></a>
<a name="line140"></a>function makeIframeDocument(iframe) {
<a name="line141"></a>  var doc = goog.dom.getFrameContentDocument(iframe);
<a name="line142"></a>  doc.open();
<a name="line143"></a>  doc.write(&#39;&lt;html&gt;&lt;head&gt;&#39;);
<a name="line144"></a>  doc.write(&#39;&lt;style&gt;html,body { background-color: transparent; }&lt;/style&gt;&#39;);
<a name="line145"></a>  doc.write(&#39;&lt;/head&gt;&lt;body&gt;&lt;/body&gt;&lt;/html&gt;&#39;);
<a name="line146"></a>  doc.close();
<a name="line147"></a>  return doc;
<a name="line148"></a>}
<a name="line149"></a>
<a name="line150"></a>function testCopyCss() {
<a name="line151"></a>  for (var i = 1; i &lt;= 4; i++) {
<a name="line152"></a>    var sourceElement = document.getElementById(&#39;source&#39; + i);
<a name="line153"></a>    var newFrame = document.createElement(&#39;iframe&#39;);
<a name="line154"></a>    newFrame.allowTransparency = true;
<a name="line155"></a>    sourceElement.parentNode.insertBefore(newFrame,
<a name="line156"></a>                                          sourceElement.nextSibling);
<a name="line157"></a>    var doc = makeIframeDocument(newFrame);
<a name="line158"></a>    goog.cssom.addCssText(
<a name="line159"></a>        goog.cssom.iframe.style.getElementContext(sourceElement),
<a name="line160"></a>        new goog.dom.DomHelper(doc));
<a name="line161"></a>    doc.body.innerHTML = sourceElement.innerHTML;
<a name="line162"></a>
<a name="line163"></a>    var oldProps = recursivelyListCssProperties(sourceElement);
<a name="line164"></a>    var newProps = recursivelyListCssProperties(doc.body);
<a name="line165"></a>
<a name="line166"></a>    assertEquals(oldProps.length, newProps.length);
<a name="line167"></a>    for (var j = 0; j &lt; oldProps.length; j++) {
<a name="line168"></a>      for (var k = 0; k &lt; propertiesToTest.length; k++) {
<a name="line169"></a>        assertEquals(&#39;testing property &#39; + propertiesToTest[k],
<a name="line170"></a>                     oldProps[j][0][propertiesToTest[k]],
<a name="line171"></a>                     newProps[j][0][propertiesToTest[k]]);
<a name="line172"></a>      }
<a name="line173"></a>    }
<a name="line174"></a>  }
<a name="line175"></a>}
<a name="line176"></a>
<a name="line177"></a>function normalizeCssText(cssText) {
<a name="line178"></a>  // Normalize cssText for testing purposes.
<a name="line179"></a>  return cssText.replace(/\s/g, &#39;&#39;).toLowerCase();
<a name="line180"></a>}
<a name="line181"></a>
<a name="line182"></a>function testAImportantInFF2() {
<a name="line183"></a>  var testDiv = document.getElementById(&#39;source1&#39;);
<a name="line184"></a>  var cssText = normalizeCssText(
<a name="line185"></a>      goog.cssom.iframe.style.getElementContext(testDiv));
<a name="line186"></a>  var color = standardizeCSSValue(&#39;color&#39;, &#39;red&#39;);
<a name="line187"></a>  var NORMAL_RULE = &#39;a{color:&#39; + color;
<a name="line188"></a>  var FF_2_RULE = &#39;a{color:&#39; + color + &#39;!important&#39;;
<a name="line189"></a>  if (goog.userAgent.GECKO &amp;&amp; !goog.userAgent.isVersionOrHigher(&#39;1.9a&#39;)) {
<a name="line190"></a>    assertContains(FF_2_RULE, cssText);
<a name="line191"></a>  } else {
<a name="line192"></a>    assertContains(NORMAL_RULE, cssText);
<a name="line193"></a>    assertNotContains(FF_2_RULE, cssText);
<a name="line194"></a>  }
<a name="line195"></a>}
<a name="line196"></a>
<a name="line197"></a>function testCopyBackgroundContext() {
<a name="line198"></a>  var testDiv = document.getElementById(&#39;backgroundTest&#39;);
<a name="line199"></a>  var cssText = goog.cssom.iframe.style.getElementContext(testDiv,
<a name="line200"></a>                                                          null,
<a name="line201"></a>                                                          true);
<a name="line202"></a>  var iframe = document.createElement(&#39;iframe&#39;);
<a name="line203"></a>  var ancestor = document.getElementById(&#39;backgroundTest-ancestor-1&#39;);
<a name="line204"></a>  ancestor.parentNode.insertBefore(iframe, ancestor.nextSibling);
<a name="line205"></a>  iframe.style.width = &#39;100%&#39;;
<a name="line206"></a>  iframe.style.height = &#39;100px&#39;;
<a name="line207"></a>  iframe.style.borderWidth = &#39;0px&#39;;
<a name="line208"></a>  var doc = makeIframeDocument(iframe);
<a name="line209"></a>  goog.cssom.addCssText(cssText, new goog.dom.DomHelper(doc));
<a name="line210"></a>  doc.body.innerHTML = testDiv.innerHTML;
<a name="line211"></a>  var normalizedCssText = normalizeCssText(cssText);
<a name="line212"></a>  assertTrue(
<a name="line213"></a>      &#39;Background color should be copied from parent element&#39;,
<a name="line214"></a>      /body{[^{]*background-color:(?:rgb\(128,0,128\)|#800080)/.test(
<a name="line215"></a>          normalizedCssText));
<a name="line216"></a>  assertTrue(
<a name="line217"></a>      &#39;Background image should be copied from ancestor element&#39;,
<a name="line218"></a>      /body{[^{]*background-image:url\(/.test(normalizedCssText));
<a name="line219"></a>  // Background-position can&#39;t be calculated in FF2, due to this bug:
<a name="line220"></a>  // http://bugzilla.mozilla.org/show_bug.cgi?id=316981
<a name="line221"></a>  if (!(goog.userAgent.GECKO &amp;&amp; !goog.userAgent.isVersionOrHigher(&#39;1.9&#39;))) {
<a name="line222"></a>    // Expected x position is:
<a name="line223"></a>    // originalBackgroundPositionX - elementOffsetLeft
<a name="line224"></a>    // 40px - (1px + 8px) == 31px
<a name="line225"></a>    // Expected y position is:
<a name="line226"></a>    // originalBackgroundPositionY - elementOffsetLeft
<a name="line227"></a>    // 70px - (1px + 10px + 5px) == 54px;
<a name="line228"></a>    assertTrue(&#39;Background image position should be adjusted correctly&#39;,
<a name="line229"></a>        /body{[^{]*background-position:31px54px/.test(normalizedCssText));
<a name="line230"></a>  }
<a name="line231"></a>}
<a name="line232"></a>
<a name="line233"></a>function testCopyBackgroundContextFromIframe() {
<a name="line234"></a>  var testDiv = document.getElementById(&#39;backgroundTest&#39;);
<a name="line235"></a>  var iframe = document.createElement(&#39;iframe&#39;);
<a name="line236"></a>  iframe.allowTransparency = true;
<a name="line237"></a>  iframe.style.position = &#39;absolute&#39;;
<a name="line238"></a>  iframe.style.top = &#39;5px&#39;;
<a name="line239"></a>  iframe.style.left = &#39;5px&#39;;
<a name="line240"></a>  iframe.style.borderWidth = &#39;2px&#39;;
<a name="line241"></a>  iframe.style.borderStyle = &#39;solid&#39;;
<a name="line242"></a>  testDiv.appendChild(iframe);
<a name="line243"></a>  var doc = makeIframeDocument(iframe);
<a name="line244"></a>  doc.body.backgroundColor = &#39;transparent&#39;;
<a name="line245"></a>  doc.body.style.margin = &#39;0&#39;;
<a name="line246"></a>  doc.body.style.padding = &#39;0&#39;;
<a name="line247"></a>  doc.body.innerHTML = &#39;&lt;p style=&quot;margin: 0&quot;&gt;I am transparent!&lt;/p&gt;&#39;;
<a name="line248"></a>  var normalizedCssText = normalizeCssText(
<a name="line249"></a>      goog.cssom.iframe.style.getElementContext(
<a name="line250"></a>          doc.body.firstChild, null, true));
<a name="line251"></a>  // Background properties should get copied through from the parent
<a name="line252"></a>  // document since the iframe is transparent
<a name="line253"></a>  assertTrue(
<a name="line254"></a>      &#39;Background color should be copied from parent element&#39;,
<a name="line255"></a>      /body{[^{]*background-color:(?:rgb\(128,0,128\)|#800080)/.test(
<a name="line256"></a>          normalizedCssText));
<a name="line257"></a>  assertTrue(
<a name="line258"></a>      &#39;Background image should be copied from ancestor element&#39;,
<a name="line259"></a>      /body{[^{]*background-image:url\(/.test(normalizedCssText));
<a name="line260"></a>  // Background-position can&#39;t be calculated in FF2, due to this bug:
<a name="line261"></a>  // http://bugzilla.mozilla.org/show_bug.cgi?id=316981
<a name="line262"></a>  if (!(goog.userAgent.GECKO &amp;&amp; !goog.userAgent.isVersionOrHigher(&#39;1.9&#39;))) {
<a name="line263"></a>    // Image offset should have been calculated to be the same as the
<a name="line264"></a>    // above example, but adding iframe offset and borderWidth.
<a name="line265"></a>    // Expected x position is:
<a name="line266"></a>    // originalBackgroundPositionX - elementOffsetLeft
<a name="line267"></a>    // 40px - (1px + 8px + 5px + 2px) == 24px
<a name="line268"></a>    // Expected y position is:
<a name="line269"></a>    // originalBackgroundPositionY - elementOffsetLeft
<a name="line270"></a>    // 70px - (1px + 10px + 5px + 5px + 2px) == 47px;
<a name="line271"></a>    assertTrue(&#39;Background image position should be adjusted correctly&#39;,
<a name="line272"></a>        !!/body{[^{]*background-position:24px47px/.exec(
<a name="line273"></a>            normalizedCssText));
<a name="line274"></a>  }
<a name="line275"></a>  iframe.parentNode.removeChild(iframe);
<a name="line276"></a>}
<a name="line277"></a>
<a name="line278"></a>function testCopyFontFaceRules() {
<a name="line279"></a>  var isFontFaceCssomSupported =
<a name="line280"></a>      goog.userAgent.WEBKIT ||
<a name="line281"></a>      goog.userAgent.OPERA ||
<a name="line282"></a>      (goog.userAgent.GECKO &amp;&amp; goog.userAgent.isVersionOrHigher(&#39;1.9.1&#39;));
<a name="line283"></a>  // We cannot use goog.testing.ExpectedFailures since it dynamically
<a name="line284"></a>  // brings in CSS which causes the background context tests to fail
<a name="line285"></a>  // in IE6.
<a name="line286"></a>  if (isFontFaceCssomSupported) {
<a name="line287"></a>    var cssText = goog.cssom.iframe.style.getElementContext(
<a name="line288"></a>        document.getElementById(&#39;cavalier&#39;));
<a name="line289"></a>    assertTrue(&#39;The font face rule should have been copied correctly&#39;,
<a name="line290"></a>               /@font-face/.test(cssText));
<a name="line291"></a>  }
<a name="line292"></a>}
</pre>


</body>
</html>
